let uuid;
let data;
let ques;
let wenjuan;
window.onload = function () {

    uuid = getParams("uuid");
    data = JSON.parse(localStorage.getItem("questionData"))
    wenjuan = data[uuid]
    document.getElementById("title").innerHTML += wenjuan.questionTitle

    ques = JSON.parse(wenjuan.problems)
    for (let index = 0; index < ques.length; index++) {
        const elem = ques[index];
        let qtitle = elem.problemType
        let ht = ""
        let tim = elem.problem;
        let xuanxiang = "";
        let det = elem.detail
        if (qtitle == "1") {
            xuanxiang += "<h3>" + tim + "(单选题)</h3>"
            for (let index2 = 0; index2 < det.length; index2++) {
                xuanxiang += "<div><input type=\"radio\" name=\""+(index+1)+"\" value=\""+det[index2]+"\"><span>"+det[index2]+"</span></div>"
            }

        } else if (qtitle == "2") {
            xuanxiang += "<h3>" + tim + "(多选题)</h3>";
            for (let index2 = 0; index2 < det.length; index2++) {
                xuanxiang += "<div><input type=\"checkbox\" name=\""+(index+1)+"\" value=\""+det[index2]+"\"><span>"+det[index2]+"</span></div>"
            }

        } else {
            xuanxiang = "<h3>" + tim + "(文本题)</h3>\
            <div><textarea name=\""+(index+1)+"\" \
            cols=\"50\" rows=\"2\"></textarea></div>"
        }

        ht = "\
            <di class=\"question_box\">\
                <div>Q" + (index + 1) + "</div>\
                <div class=\"question_manner\">\
                "+xuanxiang+"\
                </div>\
            </di\
            v>"; 


        document.getElementById("addti").innerHTML += ht;
    }

}

// 获取url里key后面的值
function getParams(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
};

$.fn.serializeObject = function () {
    var ct = this.serializeArray();
    var obj = {};
    $.each(ct, function () {
        if (obj[this.name] !== undefined) {
            if (!obj[this.name].push) {
                obj[this.name] = [obj[this.name]];
            }
            obj[this.name].push(this.value || "");
        } else {
            obj[this.name] = this.value || "";
        }
    });
    return obj;
};

// 初始化问卷数据
function initwenjuan() {
    let wenjuanlist = new Array();
    for (let index = 0; index < ques.length; index++) {
        const elem = ques[index];
        let det = elem.detail;
        let xuanans = {};
        if (elem.problemType == "3") {
            xuanans.writerNum = 0;
            xuanans["datatext"] = new Array();
        } else {
            for (let index2 = 0; index2 < det.length; index2++) {
                xuanans[det[index2]] = 0
            }
        }
        wenjuanlist.push(xuanans)
    }
    wenjuan.data = wenjuanlist
}

function getform() {
    var formdata = $("#biaodan").serializeObject();
    let ans = wenjuan.data;
    if (ans == null) {
        initwenjuan();
        ans = wenjuan.data;
    } 

    // 存数据
    for (let index = 0; index < ques.length; index++) {
        let sjk = ans[index];
        let tianxieshuju = formdata[index+1];
        
        // console.log(sjk);
        // console.log(tianxieshuju);
        // console.log("---------");

        // 为空跳过
        if (tianxieshuju == null) {
            continue;
        }

        if (tianxieshuju instanceof Array) {
            // 多选
            for (let index2 = 0; index2 < tianxieshuju.length; index2++) {
                let sel = tianxieshuju[index2];
                sjk[sel] += 1
            }
        } else {
            if (sjk.datatext == null) {
                // 单选
                sjk[tianxieshuju] += 1
            } else {
                // 文本
                sjk.writerNum += 1
                if (tianxieshuju != "") {
                    sjk.datatext.push(tianxieshuju);
                }
            }
        }

    }
    console.log(ans);
    wenjuan.data = ans;
    data[uuid] = wenjuan;
    localStorage.setItem("questionData", JSON.stringify(data))
    // console.log(data);
    // debugger
    location.href = "index.html"
}